﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace CarbonExchange.Bll
{
    public class AmeeDataItemFactory : AmeeFactoryBase<AmeeDataItem>
    {

        public AmeeDataItem GetByPath(string path, string id)
        {
            XDocument doc = Command.SendXmlGet(BuildAmeeDataPath(path, id));
            var item = doc.Root.Element("DataItemResource");
            if (item != null)
            {
                var tmp = new AmeeDataItem();
                tmp.Id = item.Element("DataItem").Attribute("uid").Value;
                tmp.Created = ConvertToAmeeDate(item.Element("DataItem").Attribute("created").Value);
                tmp.Name = item.Element("DataItem").Element("Name").Value;
                tmp.Path = item.Element("Path").Value;
                tmp.ItemValues = item.Element("DataItem").Element("ItemValues").Elements("ItemValue").Select(x => new AmeeProfileItemProperty(x.Element("ItemValueDefinition").Element("ValueDefinition").Element("ValueType").Value, x.Element("Name").Value, x.Element("Value").Value, x.Element("Path").Value)).ToList();
                return tmp;
            }
            else
            {
                return null;
            }
        }

    }
}
